## ----setup, include=FALSE-------------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
library(data.table)
library(ggplot2)
library(fixest)
library(modelsummary)
library(fst)
options(scipen = 999)
setFixest_nthreads(14)
setDTthreads(14)
folder <- "" # Add here the folder for the tables.


## ----Fixest_Table_Default, echo=FALSE-------------------------------------------
setFixest_etable(
  digits = 3, digits.stats = 3, fitstat = ~n,
  coefstat = "se", ci = 0.95, se.below = TRUE, depvar = FALSE,
  style.tex =
    style.tex(
      var.title = "\\midrule",
      depvar.title = "",
      model.title = "",
      fixef.title = "\\midrule Fixed-Effects",
      stats.title = "\\midrule",
      yesNo = c("Yes", "No"),
      tablefoot.value = ""
    )
)


## ----Data_Aggregation, echo=FALSE, include=FALSE, warning=FALSE-----------------
TIM_raw <- as.data.table(haven::read_dta("TIM_transactions.dta"))
setnames(TIM_raw, c("importer"), c("country"))
#Firm-product-customs-destination-semester-year dataset
TIM_FPCAS <- TIM_raw[, .(
  exp_tfpca = sum(exp_value),
  wei_tfpca = sum(wei_value),
  nb_tfpca = uniqueN(importer),
  nt_tfpca = .N,
  p_tim_tfpca = max(tim_use)
),
by = list(id_firm, product, munic, custom, country, year, semester)
]
outcomes <- c('exp_tfpca', 'wei_tfpca', 'nt_tfpca', 'nb_tfpca')
loutcomes <- paste("l", outcomes, sep="")
TIM_FPCAS[, (loutcomes) := lapply(.SD, log), .SDcols = outcomes ]
TIM_FPCAS[, cod_dep := stringr::str_sub(munic, 1, 2)]
## Fixed Effects definition
TIM_FPCAS[, FPCA := .GRP, by = list(id_firm, product, country, custom)]
TIM_FPCAS[, MCA := .GRP, by = .(munic, custom, country)]
TIM_FPCAS[, PCT := .GRP, by = list(country, product, year)]
TIM_FPCAS[order(year, semester), seq := .GRP, by = .(year, freq)]
TIM_FPCAS[, MT := .GRP, by = .(munic, year)]
TIM_FPCAS[, MS := .GRP, by = .(munic, seq)]
TIM_FPCAS[, PT := .GRP, by = .(product, year)]
TIM_FPCAS[, CT := .GRP, by = .(country, year)]
write.fst(TIM_FPCAS, 'TIM_FPCAS_0713.fst')

#Firm-product-customs-destination-year dataset
TIM_FPCA <- TIM_raw[, .(
  exp_tfpca = sum(exp),
  nt_tfpca = .N,
  p_tim_tfpca = max(p_tim)
),
by = list(id_firm, product, munic, custom, country, year, freq)
]
outcomes <- c('exp_tfpca', 'wei_tfpca', 'nt_tfpca', 'nb_tfpca')
loutcomes <- paste("l", outcomes, sep="")
TIM_FPCA[, (loutcomes) := lapply(.SD, log), .SDcols = outcomes ]
TIM_FPCA[, cod_dep := stringr::str_sub(munic, 1, 2)]
## Fixed Effects definition
TIM_FPCA[, FPCA := .GRP, by = list(id_firm, product, country, custom)]
TIM_FPCA[, MCA := .GRP, by = .(munic, custom, country)]
TIM_FPCA[, PCT := .GRP, by = list(country, product, year)]
TIM_FPCA[, MT := .GRP, by = .(munic, year)]
TIM_FPCA[, PT := .GRP, by = .(product, year)]
TIM_FPCA[, CT := .GRP, by = .(country, year)]
write.fst(TIM_FPCA, 'TIM_FPCA_0713.fst')

#Municipality-customs-destination-semester-year dataset
TIM <- as.data.table(read.fst("TIM_FPCAS_0713.fst"))
TIM[, FP := .GRP, by = list(id_firm, product)]
TIM_MCAS <- TIM[, .(
  N_F = log(uniqueN(id_firm)),
  N_FE = log(uniqueN(FP)),
  X = log(sum(exp_tfpca)),
  T = max(p_tim_tfpca)
),
by = list(munic, custom, country, year, freq)
]
## Fixed Effects definition
TIM_MCAS[order(year, semester), seq := .GRP, by = .(year, freq)]
TIM_MCAS[, MCA := .GRP, by = list(munic, country, custom)]
TIM_MCAS[, CT := .GRP, by = list(country, year)]
TIM_MCAS[, MT := .GRP, by = list(munic, year)]
TIM_MCAS[, MS := .GRP, by = list(munic, seq)]
TIM_MCAS[, AT := .GRP, by = list(custom, year)]
## Additional Outcomes
TIM_MCAS[, `:=`(NFExNF = N_FE - N_F, XxNFE = X - N_FE, XxNF = X - N_F)]
write.fst(TIM_MCAS, 'TIM_MCAS_0713.fst')

#Municipality-customs-destination-semester-year dataset
TIM <- as.data.table(read.fst("TIM_FPCA_0713.fst"))
TIM[, FP := .GRP, by = list(id_firm, product)]
TIM_MCA <- TIM[, .(
  N_F = log(uniqueN(id_firm)),
  N_FE = log(uniqueN(FP)),
  X = log(sum(exp_tfpca)),
  T = max(p_tim_tfpca)
),
by = list(munic, custom, country, year)
]
## Fixed Effects definition
TIM_MCA[, MCA := .GRP, by = list(munic, country, custom)]
TIM_MCA[, CT := .GRP, by = list(country, year)]
TIM_MCA[, MT := .GRP, by = list(munic, year)]
TIM_MCA[, AT := .GRP, by = list(custom, year)]
## Additional Outcomes
TIM_MCA[, `:=`(NFExNF = N_FE - N_F, XxNFE = X - N_FE, XxNF = X - N_F)]
write.fst(TIM_MCA, 'TIM_MCA_0713.fst')


## ----MCAS_Bench, echo=FALSE, include=FALSE, warning=FALSE-----------------------
setFixest_fml(..FE1 = ~ MCA + seq)
setFixest_fml(..FE2 = ~ MCA + CT + seq)
setFixest_fml(..FE3 = ~ MCA + CT + AT + seq)
setFixest_fml(..FE4 = ~ MCA + CT + AT + MT + seq)
TIM_MCA <- as.data.table(read.fst("TIM_MCAS_0713.fst"))
models <- list()
models[["FE1"]] <- feols(X ~ T | ..FE1, TIM_MCA[year > 2009])
models[["FE2"]] <- feols(X ~ T | ..FE2, TIM_MCA[year > 2009])
models[["FE3"]] <- feols(X ~ T | ..FE3, TIM_MCA[year > 2009])
models[["FE4"]] <- feols(X ~ T | ..FE4, TIM_MCA[year > 2009])
name <- paste(folder, "Table_2_Final.tex", sep = "")
etable(models, file = name, tex = TRUE, replace = TRUE)


## ----MCAS_Decomp_Table, echo=FALSE, include=FALSE-------------------------------
models <- list()
models[["Exports"]] <- feols(X ~ T | ..FE4, TIM_MCA[year > 2009])
models[["Number of Firms"]] <- feols(N_F ~ T | ..FE4, TIM_MCA[year > 2009])
models[["Exports per Firm-Prod"]] <- feols(XxNFE ~ T | ..FE4, TIM_MCA[year > 2009])
models[["Number of Firm-Prod per Firm"]] <- feols(NFExNF ~ T | ..FE4, TIM_MCA[year > 2009])
models[["Exports per Firm"]] <- feols(XxNF ~ T | ..FE4, TIM_MCA[year > 2009])


## ----MCAS_Decomp_Table_tex, echo=FALSE------------------------------------------
etable(models, file = name, tex = TRUE, replace = TRUE)
Table_3 <- readLines(name)
Table_ml1 <- "& Total & Number of& Number of & Exports per  & Exports per\\\\"
Table_ml2 <- "& Exports & Firms & Firm-Product& Firm-Product & Firm\\\\"
Table_ml3 <- "& & & per Firm &  & \\\\"
Table_ml4 <- "\\midrule"
Table_3 <- c(
  Table_3[1:2], Table_ml1, Table_ml2, Table_ml3, Table_3[5:13],
  Table_ml4, Table_ml4,
  Table_3[15]
)
name <- paste(folder, "Table_3_Final.tex", sep = "")
writeLines(Table_3, name)


## ----MCA_Placebo_Xgr, echo=FALSE------------------------------------------------
# We estimate the placebo and export growth here to later construct Table 5
TIM_MCA <- panel(TIM_MCA, ~ MCA + seq)
TIM_MCA[, `:=`(
  F1T = f(T, 1),
  F2T = f(T, 2)
)]
TIM_MCA[, `:=`(Xgr = X - l(X, 1, fill = 0))]
TIM_MCA[, `:=`(
  L1Xgr = l(Xgr, 1),
  L2Xgr = l(Xgr, 2)
)]
TIM_MCA[T == 1, effyear := seq]
TIM_MCA[T == 0, effyear := 999999]
TIM_MCA[, `:=`(meffyear = min(effyear)), by = list(munic, custom, country)]
TIM_MCA[meffyear == 999999, meffyear := 0]
TIM_MCA[, time_to_treatment := seq - meffyear]
Placebos_MCA <-
  feols(X~ F1T + F2T |
    ..FE4,
  cluster = ~MCA,
  TIM_MCA[T == 0 & year > 2009]
  )
models_Xgr_MCA <-
  feols(
    T ~ L1Xgr + L2Xgr |
      ..FE4,
    TIM_MCA[(T == 0 | T == 1 & seq <= meffyear) & year > 2009]
  )


## ----FPCA_Table_4, echo = FALSE-------------------------------------------------
setFixest_fml(..FE1 = ~ FPCA + seq)
setFixest_fml(..FE2 = ~ FPCA + PCT + AT + seq)
setFixest_fml(..FE3 = ~ FPCA + PCT + AT + MT + seq)
setFixest_fml(..FE4 = ~ FPCA + PCT + AT + FT + seq)
TIM <- as.data.table(read.fst("TIM_FPCAS_0713.fst"))
TIM <- panel(TIM, ~ FPCA + seq)
# Lagged Indicators
TIM[, `:=`(
  F1_0p_tim_tfpca = f(p_tim_tfpca, 1, fill = 0),
  F2_0p_tim_tfpca = f(p_tim_tfpca, 2, fill = 0)
), ]
TIM[, `:=`(
  F1q_tim_tmca = max(F1_0p_tim_tfpca),
  F2q_tim_tmca = max(F2_0p_tim_tfpca)
),
by = .(munic, country, custom, year, freq)
]
TIM[, `:=`(
  F1q_tim_tdca = max(F1_0p_tim_tfpca),
  F2q_tim_tdca = max(F2_0p_tim_tfpca)
),
by = .(cod_dep, country, custom, year, freq)
]
TIM[year == 2013, `:=`(
  F2q_tim_tmca = NA,
  F2q_tim_tdca = NA
)]
TIM[year == 2013 & freq == 2, `:=`(
  F1q_tim_tmca = NA,
  F1q_tim_tdca = NA
)]
TIM[, q_tim_tmca := max(p_tim_tfpca), by = .(munic, country, custom, seq)]
TIM[, q_tim_tdca := max(p_tim_tfpca), by = .(cod_dep, country, custom, seq)]

modelsOLS <- list()
modelsITT <- list()
modelsIV <- list()
modelsOLS[["FE1"]] <-
  feols(lexp_tfpca ~ p_tim_tfpca | ..FE1, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsITT[["FE1"]] <-
  feols(lexp_tfpca ~ q_tim_tmca | ..FE1, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsIV[["FE1"]] <-
  feols(lexp_tfpca ~ 1 | ..FE1 | p_tim_tfpca ~ q_tim_tmca, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsOLS[["FE2"]] <-
  feols(lexp_tfpca ~ p_tim_tfpca | ..FE2, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsITT[["FE2"]] <-
  feols(lexp_tfpca ~ q_tim_tmca | ..FE2, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsIV[["FE2"]] <-
  feols(lexp_tfpca ~ 1 | ..FE2 | p_tim_tfpca ~ q_tim_tmca, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsOLS[["FE3"]] <-
  feols(lexp_tfpca ~ p_tim_tfpca | ..FE3, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsITT[["FE3"]] <-
  feols(lexp_tfpca ~ q_tim_tmca | ..FE3, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsIV[["FE3"]] <-
  feols(lexp_tfpca ~ 1 | ..FE3 | p_tim_tfpca ~ q_tim_tmca, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsOLS[["FE4"]] <-
  feols(lexp_tfpca ~ p_tim_tfpca | ..FE4, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsITT[["FE4"]] <-
  feols(lexp_tfpca ~ q_tim_tmca | ..FE4, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")
modelsIV[["FE4"]] <-
  feols(lexp_tfpca ~ 1 | ..FE4 | p_tim_tfpca ~ q_tim_tmca, TIM[year > 2009], cluster =~MCA, fixef.rm="singleton")


## ----FPCA_Placebo_XGr, echo=FALSE-----------------------------------------------
Placebos_FPCA_ITT <-
  feols(lexp_tfpca ~ F1q_tim_tmca + F2q_tim_tmca |
    ..FE4,
  cluster = ~MCA,
  TIM[q_tim_tmca == 0 & year > 2009], fixef.rm = "singleton"
  )
Placebos_FPCA_ITT_DCA <-
  feols(lexp_tfpca ~ F1q_tim_tdca + F2q_tim_tdca |
    ..FE4,
  cluster = ~MCA,
  TIM[q_tim_tdca == 0 & year > 2009], fixef.rm = "singleton"
  )

TIM <- panel(TIM, ~ FPCA + seq)
setnames(
  TIM, c("lexp_tfpca", "p_tim_tfpca", "q_tim_tfpca", "q_tim_tmca"),
  c("X", "T", "Q_MCA", "Q_FPCA")
)
TIM <- panel(TIM, ~ FPCA + seq)
TIM[, `:=`(Xgr = X - l(X, 1))]
TIM[, `:=`(
  L1Xgr = l(Xgr, 1),
  L2Xgr = l(Xgr, 2)
)]
TIM[Q_MCA == 1, effyear := year]
TIM[Q_MCA == 0, effyear := 999999]
TIM[, `:=`(meffyear = min(effyear)), by = list(munic, custom, country)]
TIM[meffyear == 999999, meffyear := 0]
TIM[T == 1, Teffyear := seq]
TIM[T == 0, Teffyear := 999999]
TIM[, `:=`(Tmeffyear = min(Teffyear)), by = list(id_firm, custom, country)]
TIM[Tmeffyear == 999999, Tmeffyear := 0]
TIM[Q_FPCA == 1, QFeffyear := seq]
TIM[Q_FPCA == 0, QFeffyear := 999999]
TIM[, `:=`(QFmeffyear = min(QFeffyear)), by = list(munic, custom, country)]
TIM[QFmeffyear == 999999, QFmeffyear := 0]
models_Xgr_FPCA_ITT <-
  feols(Q_FPCA ~ L1Xgr + L2Xgr | ..FE4,
    TIM[(Q_FPCA == 0 | Q_FPCA == 1 & seq <= QFmeffyear) & year > 2009],
    cluster =~MCA, fixef.rm = "singleton"
  )


## ----FPCA_Table_4_tex, echo = FALSE---------------------------------------------
mITT <- modelsummary(modelsITT,
  stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
  title = "Full Sample",
  coef_rename = c(
    "p_tim_tfpca" = "TIM - OLS",
    "q_tim_tmca" = "TIM - ITT",
    "fit_p_tim_tfpca" = "TIM - IV"
  ),
  gof_omit = ".*", output = "data.frame"
)
mIV <- modelsummary(modelsIV,
  stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
  title = "Full Sample",
  coef_rename = c(
    "p_tim_tfpca" = "TIM - OLS",
    "q_tim_tmca" = "TIM - ITT",
    "fit_p_tim_tfpca" = "TIM - IV"
  ),
  gof_omit = "IC|Log|Adj|Pseudo|Within|Std.|R2", output = "data.frame"
)
mAll <- rbind(mITT, mIV[1:3, ], mIV[5, ], mIV[9, ], mIV[8, ], mIV[4, ], mIV[6:7, ])
mAll <- rbind(mAll, mAll[5, ])
mAll[2, 2] <- ""
mAll[4, 2] <- ""
mAll[5, 2] <- "Fixed-Effects"
mAll[5, 4:7] <- ""
mAll[6, 2] <- "JJFirm-Product-Customs-Destination"
mAll[7, 2] <- "JJSemester-Year"
mAll[8, 2] <- "JJProduct-Destination-Year"
mAll[9, 2] <- "JJCustoms-Year"
mAll[10, 2] <- "JJFirm-Year"
mAll[11, 2] <- "JJMunicipality-Year"
mAll[12, 2] <- "Observations"
mAll[stringr::str_sub(mAll$term, 1, 2) == "JJ" & mAll$FE1 == "", 4] <- "No"
mAll[stringr::str_sub(mAll$term, 1, 2) == "JJ" & mAll$FE2 == "", 5] <- "No"
mAll[stringr::str_sub(mAll$term, 1, 2) == "JJ" & mAll$FE3 == "", 6] <- "No"
mAll[stringr::str_sub(mAll$term, 1, 2) == "JJ" & mAll$FE4 == "", 7] <- "No"
modelsummary(modelsOLS,
  stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
  coef_rename = c(
    "p_tim_tfpca" = "TIM - OLS",
    "q_tim_tmca" = "TIM - ITT",
    "fit_p_tim_tfpca" = "TIM - IV",
    "lexp_tfpca" = "Total Exports"
  ),
  gof_omit = ".*",
  add_rows = mAll[, c(2, 4:7)], output = "Table_4.tex"
)
Table_4 <- readLines("Table_4.tex")
Table_4b <- c(Table_4[3:4], Table_4[6], Table_4[5:6], Table_4[7:12], Table_4[6], Table_4[13:19], Table_4[6], Table_4[20], Table_4[6], Table_4[6], Table_4[23])
Table_4b <- gsub("JJ", "~~", Table_4b)
Table_4b <- gsub("X", "Yes", Table_4b)
name <- paste(folder, "Table_4b.tex", sep = "")
writeLines(Table_4b, name)
# Adding First Stage information
etable(modelsIV,
  stage = 1, se.below = TRUE, fitstat = ~ . + n + ivwaldall + ivfall,
  tex = TRUE, file = "Table_4b_IV_Aux.tex", replace = TRUE
)
Table_4b_aux <- readLines("Table_4b_IV_Aux.tex")[-(1:3)]
Table_IV <- "& \\multicolumn{4}{c}{IV - First Stage}\\\\"
Table_4_F <- c(
  Table_4b[1:22], Table_IV,
  Table_4b_aux[5:6], Table_4b[23],
  Table_4b_aux[20], Table_4b[23],
  Table_4b[13:24]
)
Table_4_F <- gsub("Wald \\(IV only\\)", "F-Statistics", Table_4_F)
name <- paste(folder, "Table_4_Final.tex", sep = "")
writeLines(Table_4_F, name)


## ----Grs_Placebo_Combined, echo = FALSE-----------------------------------------
setFixest_dict(c(
  T = "TIM",
  p_tim_tfpca = "TIM",
  q_tim_tdca = "TIM",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  X = "Total Exports",
  N_F = "Number of Firms",
  p_tim_tdca = "TIM",
  NFExNF = "Number of Firm-Prod per Firm",
  XxNFE = "Exports per Firm-Prod",
  XxNF = "Exports per Firm",
  q_tim_tmca = "TIM - Availability",
  F1q_tim_tmca = "Forward 1 Semester",
  F2q_tim_tmca = "Forward 2 Semesters",
  F1p_tim_tfpca = "Forward 1 Semester",
  F2p_tim_tfpca = "Forward 2 Semesters",
  F1p_tim_tmca = "Forward 1 Semester",
  F2p_tim_tmca = "Forward 2 Semesters",
  F1T = "Forward 1 Semester",
  F2T = "Forward 2 Semesters"
))
Placebo_XGr_Selected <- list(
  Placebos_MCA, models_Xgr_MCA,
  Placebos_FPCA_ITT, models_Xgr_FPCA_ITT
)
etable(Placebo_XGr_Selected,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_5_PlaceboXGr_Selected_b.tex", replace = TRUE
)
Table_5_Aux1 <- readLines("Table_5_PlaceboXGr_Selected_b.tex")[-(1:3)]
Table_ml1 <- "& \\multicolumn{2}{c}{Equation 2} & \\multicolumn{2}{c}{Equation 4}\\\\"
Table_ml2 <- "& Placebo & Growth & Placebo & Growth\\\\"
Table_ml4 <- "\\midrule"
Table_5_All <- c(
  Table_5_Aux1[1:2], Table_ml1, Table_ml4, Table_ml2, Table_ml4,
  Table_5_Aux1[3],
  Table_5_Aux1[4:11],
  Table_5_Aux1[12:21], Table_ml4,
  Table_5_Aux1[23], Table_ml4, Table_ml4,
  Table_5_Aux1[26]
)
Table_5_All
name <- paste(folder, "Table_5_PlaceboXGr.tex", sep = "")
writeLines(Table_5_All, name)


## ----Diversion_FPCA, echo = FALSE-----------------------------------------------
setFixest_fml(..FE4 = ~ FPCA + PCT + AT + FT + seq)
TIM <- as.data.table(read.fst("TIM_FPCAS_0713.fst"))
TIM[, tim_tf := max(p_tim_tfpca), by = .(id_firm, seq)]
TIM[, q_tim_tf := max(q_tim_tmca), by = .(id_firm, seq)]
TIM[, `:=`(
  sam_a0 = 1,
  sam_a1 = 0,
  sam_a2 = 0,
  sam_a3 = 0,
  sam_a1q = 0,
  sam_a3q = 0
)]
TIM[tim_tf == 0 | (tim_tf == 1 & p_tim_tfpca == 0), sam_a1 := 1]
TIM[q_tim_tf == 0 | (q_tim_tf == 1 & q_tim_tmca == 0), sam_a1q := 1]
TIM[tim_tf == 0 | (p_tim_tfpca == 1), sam_a2 := 1]
TIM[tim_tf == 1 | (year == 2010) | (year == 2011 & freq == 1), sam_a3 := 1]
TIM[q_tim_tf == 1 | (year == 2010) | (year == 2011 & freq == 1), sam_a3q := 1]
TIM[, rfpc_a_tfp := (.N > 1), by = .(id_firm, product, country, seq)]
TIM[, rfpa_c_tfp := (.N > 1), by = .(id_firm, product, custom, seq)]
TIM <- panel(TIM, ~ FPCA + seq)
cols <- grep("^rf.*", names(TIM), value = T)
new_cols <- paste("L1", cols, sep = "_")
TIM[, (new_cols) := .(
  l(rfpc_a_tfp, 1, FALSE),
  l(rfpa_c_tfp, 1, FALSE),
)]
new_cols <- paste("M", cols, sep = "_")
TIM[, (new_cols) := lapply(.SD, max), by = .(id_firm), .SDcols = cols]
Models_Reall <- list()
Models_Reall[[1]] <-
  feols(lexp_tfpca ~ q_tim_tmca | ..FE2,
    TIM[year > 2009 & sam_a0 == 1 ],
    cluster = ~MCA, fixef.rm = 'singleton'
  )
Models_Reall[[2]] <-
  feols(lexp_tfpca ~ q_tim_tmca | ..FE2,
    TIM[year > 2009 & sam_a3q == 1 & esample_1013 == 1],
    cluster = ~MCA, fixef.rm = 'singleton'
  )
Models_Reall[[3]] <-
  feols(lexp_tfpca ~ q_tim_tf | ..FE2,
    TIM[year > 2009 & sam_a1q == 1 ],
    cluster = ~MCA, fixef.rm = 'singleton'
  )
Models_Reall[[4]] <-
  feols(lexp_tfpca ~ q_tim_tmca | ..FE2,
    TIM[L1_rfpc_a_tfp == 0 & year > 2009 ],
    cluster = ~MCA, fixef.rm = 'singleton'
  )


## ----Reallocation_Table_FPCA, echo = FALSE--------------------------------------
setFixest_dict(c(
  T = "TIM",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  p_tim_tfpca = "TIM",
  q_tim_tmca = "TIM",
  q_tim_tf = "TIM",
  tim_tf = "TIM"
))
etable(Models_Reall,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_6_Reall_b.tex", replace = TRUE
)
Table_6_Aux1 <- readLines("Table_6_Reall_b.tex")[-(1:3)]
Table_ml1 <- "& \\multicolumn{1}{c}{Full} & \\multicolumn{1}{c}{Only Flows for} & \\multicolumn{1}{c}{Only Untreated} & \\multicolumn{1}{c}{Only Single} \\\\"
Table_ml2 <- "& \\multicolumn{1}{c}{Sample} & \\multicolumn{1}{c}{Treated Firms} & \\multicolumn{1}{c}{Flows} & \\multicolumn{1}{c}{Routes Flows} \\\\"
Table_ml4 <- "\\midrule"
Table_6_All <- c(
  Table_6_Aux1[1:2],
  Table_ml1, Table_ml2, Table_ml4,
  Table_6_Aux1[3],
  Table_6_Aux1[4:11], Table_ml4,
  Table_6_Aux1[13], Table_ml4, Table_ml4,
  Table_6_Aux1[16]
)
Table_6_All
name <- paste(folder, "Table_6_Diversion_FPCA.tex", sep = "")
writeLines(Table_6_All, name)


## ----DCA_Semester, include=FALSE, warning=FALSE, echo=FALSE---------------------
setFixest_fml(..FE2 = ~ DCA + CT + AT + seq)
setFixest_fml(..FE3 = ~ DCA + CT + AT + DT + seq)
TIM <- as.data.table(read.fst("TIM_FPCAS_0713.fst"))
TIM[, cod_dep := stringr::str_sub(munic, 1, 2)]
TIM[order(year, semester), seq := .GRP, by = .(year, freq)]
TIM_DCA <- TIM[, .(
  X = log(sum(exp_tfpca)),
  T = max(p_tim_tfpca)
),
by = list(cod_dep, custom, country, year, freq, seq)
]
TIM_DCA[, DCA := .GRP, by = list(cod_dep, country, custom)]
TIM_DCA[, CT := .GRP, by = list(country, year)]
TIM_DCA[, DT := .GRP, by = list(cod_dep, year)]
TIM_DCA[, DS := .GRP, by = list(cod_dep, year, freq)]
TIM_DCA[, AT := .GRP, by = list(custom, year)]
Models_Routes <- list()
Models_Routes[[1]] <-
  feols(X ~ T |
    ..FE2,
  TIM_DCA[year > 2009],
  cluster = ~DCA, fixef.rm = 'singleton'
  )
Models_Routes[[2]] <-
  feols(X ~ T |
    ..FE3,
  TIM_DCA[year > 2009],
  cluster = ~DCA, fixef.rm = 'singleton'
  )


## ----FPCAS_Routes_Alt,include= FALSE, warning=FALSE, echo=FALSE-----------------
setFixest_fml(..FE3 = ~ FPCA + PCT + AT + MT + seq)
setFixest_fml(..FE4 = ~ FPCA + PCT + AT + FT + seq)
TIM <- as.data.table(read.fst("TIM_FPCAS_0713.fst"))
Models_Routes_FPCA <- list()
Models_Routes_FPCA[[1]] <- feols(lexp_tfpca ~ q_tim_tdca | ..FE3,
  TIM[year > 2009 ],
  cluster = ~MCA, fixef.rm = 'singleton'
)
Models_Routes_FPCA[[2]] <- feols(lexp_tfpca ~ q_tim_tdca | ..FE4,
  TIM[year > 2009 ],
  cluster = ~MCA, fixef.rm = 'singleton'
)
Models_Routes_FPCA[[3]] <- feols(lexp_tfpca ~ 1 | ..FE3 | p_tim_tfpca ~ q_tim_tdca,
  TIM[year > 2009 ],
  cluster = ~MCA, fixef.rm = 'singleton'
)
Models_Routes_FPCA[[4]] <- feols(lexp_tfpca ~ 1 | ..FE4 | p_tim_tfpca ~ q_tim_tdca,
  TIM[year > 2009 ],
  cluster = ~MCA, fixef.rm = 'singleton'
)
# Export Growth
TIM <- panel(TIM, ~ FPCA + seq)
setnames(
  TIM, c("lexp_tfpca", "p_tim_tfpca", "q_tim_tfpca", "q_tim_tdca"),
  c("X", "T", "Q_MCA", "Q_FPCA")
)
TIM <- panel(TIM, ~ FPCA + seq)
TIM[, `:=`(Xgr = X - l(X, 1))]
TIM[, `:=`(
  L1Xgr = l(Xgr, 1),
  L2Xgr = l(Xgr, 2)
)]
TIM[Q_FPCA == 1, QFeffyear := seq]
TIM[Q_FPCA == 0, QFeffyear := 999999]
TIM[, `:=`(QFmeffyear = min(QFeffyear)), by = list(munic, custom, country)]
TIM[QFmeffyear == 999999, QFmeffyear := 0]
models_Xgr_FPCA_ITT_DCA <-
  feols(Q_FPCA ~ L1Xgr + L2Xgr | ..FE4,
    TIM[(Q_FPCA == 0 | Q_FPCA == 1 & seq <= QFmeffyear) & year > 2009],
    cluster =~MCA, fixef.rm = 'singleton'
  )


## ----Combining_Routes_Robustness, echo = FALSE----------------------------------
setFixest_dict(c(
  T = "TIM",
  p_tim_tfpca = "TIM",
  q_tim_tdca = "TIM-Availability",
  q_tim_tmpca = "TIM-Availability",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  X = "Total Exports",
  N_F = "Number of Firms",
  p_tim_tdca = "TIM",
  NFExNF = "Number of Firm-Prod per Firm",
  XxNFE = "Exports per Firm-Prod",
  XxNF = "Exports per Firm",
  q_tim_tmca = "TIM - Availability",
  F1q_tim_tmca = "Forward 1 Semester",
  F2q_tim_tmca = "Forward 2 Semesters",
  F1p_tim_tfpca = "Forward 1 Semester",
  F2p_tim_tfpca = "Forward 2 Semesters",
  F1p_tim_tmca = "Forward 1 Semester",
  F2p_tim_tmca = "Forward 2 Semesters"
))
Routes_Selected <- c(Models_Routes, Models_Routes, Models_Routes_FPCA[3:4])
etable(Routes_Selected,
  stage = 1, se.below = TRUE, depvar = FALSE,
  tex = TRUE, file = "Table_7_Routes_IV.tex", replace = TRUE
)
# This new dictionary is need for the structure of the table.
setFixest_dict(c(
  T = "TIM",
  p_tim_tfpca = "TIM",
  q_tim_tdca = "TIM",
  q_tim_tmpca = "TIM-Availability",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  X = "Total Exports",
  N_F = "Number of Firms",
  p_tim_tdca = "TIM",
  NFExNF = "Number of Firm-Prod per Firm",
  XxNFE = "Exports per Firm-Prod",
  XxNF = "Exports per Firm",
  q_tim_tmca = "TIM - Availability",
  F1q_tim_tmca = "Forward 1 Semester",
  F2q_tim_tmca = "Forward 2 Semesters",
  F1p_tim_tfpca = "Forward 1 Semester",
  F2p_tim_tfpca = "Forward 2 Semesters",
  F1p_tim_tmca = "Forward 1 Semester",
  F2p_tim_tmca = "Forward 2 Semesters"
))
Routes_Selected <- c(Models_Routes, Models_Routes_FPCA)
etable(Routes_Selected,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_8_Routes_Selected.tex", replace = TRUE
)
etable(Routes_Selected)
Table_8_Aux1 <- readLines("Table_8_Routes_Selected.tex")[-(1:3)]
Table_8_Aux2 <- readLines("Table_8_Routes_IV.tex")[-(1:3)]
Table_ml1 <- "& \\multicolumn{2}{c}{Department} & \\multicolumn{4}{c}{Firm-Product}\\\\"
Table_ml2 <- "& \\multicolumn{2}{c}{OLS}  & \\multicolumn{2}{c}{ITT} &  \\multicolumn{2}{c}{IV}\\\\"
Table_IV <- "& & & & &\\multicolumn{2}{c}{IV - First Stage}\\\\"
Table_ml4 <- "\\midrule"
Table_8_All <- c(
  Table_8_Aux1[1:2], Table_ml1, Table_ml4, Table_ml2, Table_ml4,
  Table_8_Aux1[3],
  Table_8_Aux1[5:6], Table_ml4, Table_IV, Table_ml4,
  Table_8_Aux2[7:8], Table_ml4,
  Table_8_Aux2[21], Table_ml4,
  Table_8_Aux1[7:16], Table_ml4,
  Table_8_Aux1[18], Table_ml4, Table_ml4,
  Table_8_Aux1[23]
)
Table_8_All <- gsub("Wald \\(1st stage\\)", "F-Statistics", Table_8_All)
name <- paste(folder, "Table_8_Final.tex", sep = "")
writeLines(Table_8_All, name)


## ----DCAS_Placebo, echo=FALSE---------------------------------------------------
setFixest_fml(..FE2 = ~ DCA + CT + AT + seq)
setFixest_fml(..FE3 = ~ DCA + CT + AT + DT + seq)
TIM <- as.data.table(read.fst("TIM_FPCAS_0713.fst"))
TIM[, cod_dep := stringr::str_sub(munic, 1, 2)]
TIM[order(year, semester), seq := .GRP, by = .(year, freq)]
TIM_DCA <- TIM[, .(
  X = log(sum(exp_tfpca)),
  T = max(p_tim_tfpca)
),
by = list(cod_dep, custom, country, year, freq, seq)
]
TIM_DCA[, DCA := .GRP, by = list(cod_dep, country, custom)]
TIM_DCA[, CT := .GRP, by = list(country, year)]
TIM_DCA[, DT := .GRP, by = list(cod_dep, year)]
TIM_DCA[, DS := .GRP, by = list(cod_dep, year, freq)]
TIM_DCA[, AT := .GRP, by = list(custom, year)]
TIM_DCA <- panel(TIM_DCA, ~ DCA + seq)
TIM_DCA[, `:=`(
  F1T = f(T, 1),
  F2T = f(T, 2)
)]
Placebos_DCA <-
  feols(X ~ F1T + F2T |
    ..FE3,
  cluster = ~DCA, fixef.rm = 'singleton',
  TIM_DCA[T== 0 & year > 2009]
  )
# Export Growth and TIM Availability
TIM_DCA[, `:=`(Xgr = X - l(X, 1, fill = 0))]
TIM_DCA[, `:=`(
  L1Xgr = l(Xgr, 1),
  L2Xgr = l(Xgr, 2)
)]
TIM_DCA[T == 1, effyear := seq]
TIM_DCA[T == 0, effyear := 999999]
TIM_DCA[, `:=`(meffyear = min(effyear)), by = list(cod_dep, custom, country)]
TIM_DCA[meffyear == 999999, meffyear := 0]
TIM_DCA[, time_to_treatment := seq - meffyear]
models_Xgr_DCA <-
  feols(
    T ~ L1Xgr + L2Xgr |
      ..FE3, fixef.rm = 'singleton',
    TIM_DCA[(T == 0 | T == 1 & seq <= meffyear) & year > 2009]
  )


## ----Grs_Placebo_Combined_DCA, echo = FALSE-------------------------------------
setFixest_dict(c(
  T = "TIM",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  X = "Total Exports",
  N_F = "Number of Firms",
  p_tim_tdca = "TIM",
  NFExNF = "Number of Firm-Prod per Firm",
  XxNFE = "Exports per Firm-Prod",
  XxNF = "Exports per Firm",
  q_tim_tmca = "TIM - Availability",
  F1T = "Forward 1 Semester",
  F2T = "Forward 2 Semesters",
  F1q_tim_tmca = "Forward 1 Semester",
  F2q_tim_tmca = "Forward 2 Semesters",
  F1q_tim_tdca = "Forward 1 Semester",
  F2q_tim_tdca = "Forward 2 Semesters",
  F1p_tim_tfpca = "Forward 1 Semester",
  F2p_tim_tfpca = "Forward 2 Semesters",
  F1p_tim_tmca = "Forward 1 Semester",
  F2p_tim_tmca = "Forward 2 Semesters",
  F1p_tim_tdca = "Forward 1 Semester",
  F2p_tim_tdca = "Forward 2 Semesters",
  L1Xgr = "Lagged 1 Semester",
  L2Xgr = "Lagged 2 Semesters",
  L3Xgr = "Lagged 3 Semesters",
  L4Xgr = "Lagged 4 Semesters"
))
Placebo_XGr_Selected_DCA <- list(
  Placebos_DCA, models_Xgr_DCA,
  Placebos_FPCA_ITT_DCA, models_Xgr_FPCA_ITT_DCA
)
etable(Placebo_XGr_Selected_DCA,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_9_PlaceboXGr_Selected_c.tex", replace = TRUE
)
Table_9_Aux1 <- readLines("Table_9_PlaceboXGr_Selected_c.tex")[-(1:3)]
Table_ml1 <- "& \\multicolumn{2}{c}{Region} & \\multicolumn{2}{c}{Firm-Product}\\\\"
Table_ml2 <- "& \\multicolumn{2}{c}{Equation (2)} & \\multicolumn{2}{c}{Equation (4)}\\\\"
Table_ml4 <- "\\midrule"
Table_9_All <- c(
  Table_9_Aux1[1:2], Table_ml1, Table_ml4, Table_ml2, Table_ml4,
  Table_9_Aux1[3],
  Table_9_Aux1[4:11],
  Table_9_Aux1[12:21], Table_ml4,
  Table_9_Aux1[23], Table_ml4, Table_ml4,
  Table_9_Aux1[26]
)
Table_9_All
name <- paste(folder, "Table_9_Final.tex", sep = "")
writeLines(Table_9_All, name)


## ----FPCAS_Decomp,include= FALSE, warning=FALSE, echo=FALSE---------------------
# Estimation is done for Table A.4 and A.7
setFixest_fml(..FE4 = ~ FPCA + PCT + AT + FT + seq)
TIM <-
  as.data.table(read.fst("TIM_FPCAS_0713.fst"))
TIM[, HS2 := stringr::str_sub(product, 1, 2)]
TIM[, HS6 := stringr::str_sub(product, 1, 6)]
TIM[, HS4 := stringr::str_sub(product, 1, 4)]
TIM[, q_tim_tmpca := max(p_tim_tfpca), by = .(munic, product, country, custom, seq)]
TIM[, q_tim_tmh2ca := max(p_tim_tfpca), by = .(munic, HS2, country, custom, seq)]
TIM[, `:=`(
  luv_tfpca = lexp_tfpca - lwght_tfpca,
  lqXs_tfpca = lwght_tfpca - lnt_tfpca,
  lsXb_tfpca = lnt_tfpca - lnb_tfpca
)]
modelsOLS_1013 <- feols(c(
  lexp_tfpca, luv_tfpca, lqXs_tfpca,
  lsXb_tfpca, lnb_tfpca
)
~ p_tim_tfpca | ..FE4,
TIM[year > 2009 ],
cluster = ~MCA, fixef.rm = 'singleton'
)
modelsITT_1013 <- feols(c(
  lexp_tfpca, luv_tfpca, lqXs_tfpca,
  lsXb_tfpca, lnb_tfpca
)
~ q_tim_tmca | ..FE4,
TIM[year > 2009 ],
cluster = ~MCA, fixef.rm = 'singleton'
)
modelsIV_1013 <- feols(c(
  lexp_tfpca, luv_tfpca, lqXs_tfpca,
  lsXb_tfpca, lnb_tfpca
)
~ 1 | ..FE4 | p_tim_tfpca ~ q_tim_tmca,
TIM[year > 2009 ],
cluster = ~MCA, fixef.rm = 'singleton'
)
modelsITT_1013_b <- feols(c(
  lexp_tfpca, luv_tfpca, lqXs_tfpca,
  lsXb_tfpca, lnb_tfpca
)
~ q_tim_tmh2ca | ..FE4,
TIM[year > 2009 ],
cluster = ~MCA, fixef.rm = 'singleton'
)
modelsIV_1013_b <- feols(c(
  lexp_tfpca, luv_tfpca, lqXs_tfpca,
  lsXb_tfpca, lnb_tfpca
)
~ 1 | ..FE4 | p_tim_tfpca ~ q_tim_tmh2ca,
TIM[year > 2009 ],
cluster = ~MCA, fixef.rm = 'singleton'
)
modelsITT_1013_c <- feols(c(
  lexp_tfpca, luv_tfpca, lqXs_tfpca,
  lsXb_tfpca, lnb_tfpca
)
~ q_tim_tmpca | ..FE4,
TIM[year > 2009 ],
cluster = ~MCA, fixef.rm = 'singleton'
)
modelsIV_1013_c <- feols(c(
  lexp_tfpca, luv_tfpca, lqXs_tfpca,
  lsXb_tfpca, lnb_tfpca
)
~ 1 | ..FE4 | p_tim_tfpca ~ q_tim_tmpca,
TIM[year > 2009 ],
cluster = ~MCA, fixef.rm = 'singleton'
)


## ----FPCA_Mult_Inst, echo=FALSE-------------------------------------------------
setFixest_dict(c(
  T = "TIM",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  p_tim_tfpca = "TIM",
  fit_p_tim_tfpca = "TIM-IV",
  q_tim_tmca = "TIM-ITT",
  q_tim_tmh2ca = "TIM-ITT",
  q_tim_tmpca = "TIM-ITT"
))
etable(list(
  modelsITT_1013[[1]],
  modelsITT_1013_b[[1]],
  modelsITT_1013_c[[1]]
),
se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
tex = TRUE, file = "Table_Mult_Inst_Aux1.tex", replace = TRUE
)
etable(list(
  modelsIV_1013[[1]],
  modelsIV_1013_b[[1]],
  modelsIV_1013_c[[1]]
),
se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
tex = TRUE, file = "Table_Mult_Inst_Aux3.tex", replace = TRUE
)
setFixest_dict(c(
  T = "TIM",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  p_tim_tfpca = "TIM",
  q_tim_tmca = "TIM - Availability",
  q_tim_tmh2ca = "TIM - Availability",
  q_tim_tmpca = "TIM - Availability"
))
etable(list(
  modelsIV_1013[[1]],
  modelsIV_1013_b[[1]],
  modelsIV_1013_c[[1]]
),
stage = 1,
se.below = TRUE, fitstat = ~ . + n + ivwald1, depvar = FALSE,
tex = TRUE, file = "Table_Mult_Inst_Aux2.tex", replace = TRUE
)
Table_10_Aux1 <- readLines("Table_Mult_Inst_Aux1.tex")[-(1:3)]
Table_10_Aux2 <- readLines("Table_Mult_Inst_Aux2.tex")[-(1:3)]
Table_10_Aux3 <- readLines("Table_Mult_Inst_Aux3.tex")[-(1:3)]
Table_ml1 <- "& Municipality& Municipality -& Municipality - \\\\"
Table_ml2 <- "& & Industry & Product \\\\"
Table_ml3 <- "& \\multicolumn{3}{c}{IV - First Stage}\\\\"
Table_ml4 <- "\\midrule"
Table_10_All <- c(
  Table_10_Aux1[1:2],
  Table_ml1, Table_ml2, Table_ml4,
  Table_10_Aux1[3],
  Table_10_Aux1[5:6],
  Table_10_Aux3[5:6], Table_ml4, Table_ml3,
  Table_10_Aux2[5:6], Table_ml4,
  Table_10_Aux2[15], Table_ml4,
  Table_10_Aux2[14], Table_ml4, Table_ml4,
  Table_10_Aux2[19]
)
Table_10_All <- gsub("Wald \\(IV only\\)", "F-Statistics", Table_10_All)
Table_10_All
name <- paste(folder, "Table_10_Final.tex", sep = "")
writeLines(Table_10_All, name)


## ----Diversion_MCA, echo =FALSE-------------------------------------------------
TIM_MCA <- as.data.table(read.fst("TIM_MCAS_0713.fst"))
TIM_MCA[, tim_tf := max(T), by = .(munic, seq)]
TIM_MCA[, `:=`(
  sam_a0 = 1,
  sam_a1 = 0,
  sam_a2 = 0,
  sam_a3 = 0
)]
TIM_MCA[tim_tf == 0 | (tim_tf == 1 & T == 0), sam_a1 := 1]
TIM_MCA[tim_tf == 0 | (T == 1), sam_a2 := 1]
TIM_MCA[tim_tf == 1 | (year == 2010), sam_a3 := 1]
TIM_MCA[, rmc_a_tm := (.N > 1), by = .(munic, country, seq)]
TIM_MCA[, rma_c_tm := (.N > 1), by = .(munic, custom, seq)]
TIM_MCA[, rm_ac_tm := (.N > 1), by = .(munic, seq)]
TIM_MCA <- panel(TIM_MCA, ~ MCA + seq)
cols <- grep("^rm.*", names(TIM_MCA), value = T)
new_cols <- paste("L1", cols, sep = "_")
TIM_MCA[, (new_cols) := .(
  l(rmc_a_tm, 1, FALSE),
  l(rma_c_tm, 1, FALSE),
  l(rm_ac_tm, 1, FALSE)
)]
setFixest_fml(..FE3 = ~ MCA + CT + AT + seq)
setFixest_fml(..FE4 = ~ MCA + CT + AT + MT + seq)
Models_Reall <- list()
Models_Reall[[1]] <-
  feols(X ~ T | ..FE3,
    TIM_MCA[year > 2009 & sam_a0 == 1],
    cluster = ~MCA, fixef.rm = 'singleton'
  )
Models_Reall[[2]] <-
  feols(X ~ T | ..FE3,
    TIM_MCA[year > 2009 & sam_a3 == 1],
    cluster = ~MCA, fixef.rm = 'singleton'
  )
Models_Reall[[3]] <-
  feols(X ~ tim_tf | ..FE3,
    TIM_MCA[year > 2009 & sam_a1 == 1],
    cluster = ~MCA, fixef.rm = 'singleton'
  )
Models_Reall[[4]] <-
  feols(X ~ T | ..FE3,
    TIM_MCA[year > 2009 & L1_rmc_a_tm == 1],
    cluster = ~MCA, fixef.rm = 'singleton'
  )


## ----Diversion_MCA_Table, echo = FALSE------------------------------------------
setFixest_dict(c(
  T = "TIM",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  p_tim_tfpca = "TIM",
  q_tim_tmca = "TIM",
  tim_tf = "TIM"
))
Reall_selected <- Models_Reall
etable(Reall_selected,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_10_Reall.tex", replace = TRUE
)
Table_11_Aux1 <- readLines("Table_10_Reall.tex")[-(1:3)]
Table_ml1 <- "& \\multicolumn{1}{c}{Full} &
					\\multicolumn{1}{c}{Only Flows for} &
					\\multicolumn{1}{c}{Only Untreated} &
					\\multicolumn{1}{c}{Only Single} \\\\"
Table_ml2 <- "& \\multicolumn{1}{c}{Sample} &
					\\multicolumn{1}{c}{Treated Firms} &
					\\multicolumn{1}{c}{Flows} &
					\\multicolumn{1}{c}{Routes Flows} \\\\"
Table_ml4 <- "\\midrule"
Table_11_All <- c(
  Table_11_Aux1[1:2],
  Table_ml1, Table_ml2, Table_ml4,
  Table_11_Aux1[3],
  Table_11_Aux1[5:13], Table_ml4, Table_ml4,
  Table_11_Aux1[16]
)
Table_11_All
name <- paste(folder, "Table_11_Final.tex", sep = "")
writeLines(Table_11_All, name)


## ----FCA_Table, echo=FALSE, include=FALSE, warning=FALSE------------------------
setFixest_dict(c(
  T = "TIM - OLS",
  p_tim_tfca = "TIM - OLS",
  q_tim_tmca = "TIM - ITT",
  fit_p_tim_tfpca = "TIM - IV",
  fit_T = "TIM - IV",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  FCA = "~~Firm-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  exp_tfca = "Total Exports",
  N_F = "Number of Firms",
  F = "~~Firm",
  p_tim_tdca = "TIM",
  NFExNF = "Number of Firm-Prod per Firm",
  XxNFE = "Exports per Firm-Prod",
  XxNF = "Exports per Firm",
  F1q_tim_tmca = "Forward 1 Semester",
  F2q_tim_tmca = "Forward 2 Semesters",
  F1p_tim_tfpca = "Forward 1 Semester",
  F2p_tim_tfpca = "Forward 2 Semesters",
  F1p_tim_tmca = "Forward 1 Semester",
  F2p_tim_tmca = "Forward 2 Semesters"
))
TIM <- as.data.table(read.fst("TIM_FPCAS_0713.fst"))
TIM_FCA <- TIM[, .(
  exp_tfca = log(sum(exp_tfpca)),
  tim_tfca = max(p_tim_tfpca)
),
by = list(id_firm, country, custom, munic, year, freq)
]
TIM_FCA[order(year, semester), seq := .GRP, by = .(year, freq)]
TIM_FCA[, FCA := .GRP, by = list(id_firm, country, custom)]
TIM_FCA[, MCA := .GRP, by = list(munic, country, custom)]
TIM_FCA[, CT := .GRP, by = list(country, year)]
TIM_FCA[, AT := .GRP, by = list(custom, year)]
TIM_FCA[, MT := .GRP, by = list(munic, year)]
TIM_FCA[, FT := .GRP, by = list(id_firm, year)]
TIM_FCA[, q_tim_tmca := max(tim_tfca), by = .(munic, custom, country, seq)]
setFixest_fml(..FE1 = ~ FCA + seq)
setFixest_fml(..FE2 = ~ FCA + CT + seq)
setFixest_fml(..FE3 = ~ FCA + CT + AT + seq)
setFixest_fml(..FE4 = ~ FCA + CT + AT + MT + seq)
setFixest_fml(..FE5 = ~ FCA + CT + AT + FT + seq)
modelsOLS <- list()
modelsOLS[["FE1"]] <- feols(exp_tfca ~ tim_tfca | ..FE1, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsOLS[["FE2"]] <- feols(exp_tfca ~ tim_tfca | ..FE2, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsOLS[["FE3"]] <- feols(exp_tfca ~ tim_tfca | ..FE3, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsOLS[["FE4"]] <- feols(exp_tfca ~ tim_tfca | ..FE4, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsOLS[["FE5"]] <- feols(exp_tfca ~ tim_tfca | ..FE5, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsITT <- list()
modelsITT[["FE1"]] <- feols(exp_tfca ~ q_tim_tfca | ..FE1, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsITT[["FE2"]] <- feols(exp_tfca ~ q_tim_tfca | ..FE2, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsITT[["FE3"]] <- feols(exp_tfca ~ q_tim_tfca | ..FE3, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsITT[["FE4"]] <- feols(exp_tfca ~ q_tim_tfca | ..FE4, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsITT[["FE5"]] <- feols(exp_tfca ~ q_tim_tfca | ..FE5, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsIV <- list()
modelsIV[["FE1"]] <- feols(exp_tfca ~ 1 | ..FE1 | tim_tfca ~ q_tim_tfca, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsIV[["FE2"]] <- feols(exp_tfca ~ 1 | ..FE2 | tim_tfca ~ q_tim_tfca, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsIV[["FE3"]] <- feols(exp_tfca ~ 1 | ..FE3 | tim_tfca ~ q_tim_tfca, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsIV[["FE4"]] <- feols(exp_tfca ~ 1 | ..FE4 | tim_tfca ~ q_tim_tfca, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')
modelsIV[["FE5"]] <- feols(exp_tfca ~ 1 | ..FE5 | tim_tfca ~ q_tim_tfca, TIM_FCA[year > 2009], cluster = ~MCA, fixef.rm='singleton')

etable(modelsOLS,
  signifCode = c("***" = 0.01, "**" = 0.05, "*" = 0.10), se.below = TRUE,
  tex = TRUE, file = "Table_aux1.tex", replace = TRUE
)
etable(modelsITT,
  signifCode = c("***" = 0.01, "**" = 0.05, "*" = 0.10), se.below = TRUE,
  tex = TRUE, file = "Table_aux2.tex", replace = TRUE
)
etable(modelsIV,
  signifCode = c("***" = 0.01, "**" = 0.05, "*" = 0.10), se.below = TRUE,
  tex = TRUE, file = "Table_aux3.tex", replace = TRUE
)
etable(modelsIV,
  stage = 1, signifCode = c("***" = 0.01, "**" = 0.05, "*" = 0.10), se.below = TRUE,
  tex = TRUE, file = "Table_aux4.tex", fitstat = ~ . + ivwald1, replace = TRUE
)

Table_aux1 <- readLines("Table_aux1.tex")[-(1:3)]
Table_aux2 <- readLines("Table_aux2.tex")[-(1:3)]
Table_aux3 <- readLines("Table_aux3.tex")[-(1:3)]
Table_aux4 <- readLines("Table_aux4.tex")[-(1:3)]
Table_aux4 <- gsub("TIM - ITT", "TIM - Availability", Table_aux4)
Table_ml <- "\\midrule"
Table_IV <- "& \\multicolumn{5}{c}{IV - First Stage}\\\\"
Table <- c(
  Table_aux1[1:3], Table_ml,
  Table_aux1[5:6],
  Table_aux2[5:6],
  Table_aux3[5:6], Table_ml,
  Table_IV, Table_ml,
  Table_aux4[5:6], Table_ml,
  Table_aux4[16], Table_ml,
  Table_aux3[7:15], Table_ml, Table_ml,
  Table_aux3[19]
)
Table <- gsub("\\\\midrule TIM", "TIM", Table)
Table <- gsub("Wald \\(IV only\\)", "F-Statistics", Table)
name <- paste(folder, "Table_12_Final.tex", sep = "")
writeLines(Table, name)


## ----FPCA_Decomp_Mult_Inst, echo=FALSE------------------------------------------
setFixest_fml(..FE1 = ~ FPCA + seq)
setFixest_fml(..FE2 = ~ FPCA + PCT + AT + seq)
setFixest_fml(..FE3 = ~ FPCA + PCT + AT + MT + seq)
setFixest_fml(..FE4 = ~ FPCA + PCT + AT + FT + seq)
setFixest_dict(c(
  T = "TIM",
  CA = "~~Custom-Destination",
  DCA = "~~Region-Custom-Destination",
  MCA = "~~Municipality-Custom-Destination",
  FPCA = "~~Firm-Product-Custom-Destination",
  seq = "~~Semester-Year",
  CT = "~~Destination-Year",
  PCT = "~~Product-Destination-Year",
  AT = "~~Custom-Year",
  MT = "~~Municipality-Year",
  DT = "~~Region-Year",
  FT = "~~Firm-Year",
  p_tim_tfpca = "TIM - OLS",
  fit_p_tim_tfpca = "TIM - IV",
  q_tim_tmca = "TIM - ITT",
  q_tim_tmh2ca = "TIM - ITT",
  q_tim_tmpca = "TIM - ITT"
))
etable(modelsITT_1013,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_Mult_Inst_Aux4.tex", replace = TRUE
)
etable(modelsITT_1013_b,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_Mult_Inst_Aux5.tex", replace = TRUE
)
etable(modelsITT_1013_c,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_Mult_Inst_Aux6.tex", replace = TRUE
)
etable(modelsIV_1013,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_Mult_Inst_Aux1.tex", replace = TRUE
)
etable(modelsIV_1013_b,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_Mult_Inst_Aux2.tex", replace = TRUE
)
etable(modelsIV_1013_c,
  se.below = TRUE, fitstat = ~ . + n, depvar = FALSE,
  tex = TRUE, file = "Table_Mult_Inst_Aux3.tex", replace = TRUE
)

Table_Aux1 <- readLines("Table_Mult_Inst_Aux1.tex")[-(1:3)]
Table_Aux1 <- gsub("TIM - IV", "TIM - IV1", Table_Aux1)
Table_Aux2 <- readLines("Table_Mult_Inst_Aux2.tex")[-(1:3)]
Table_Aux2 <- gsub("TIM - IV", "TIM - IV2", Table_Aux2)
Table_Aux3 <- readLines("Table_Mult_Inst_Aux3.tex")[-(1:3)]
Table_Aux3 <- gsub("TIM - IV", "TIM - IV3", Table_Aux3)
Table_Aux4 <- readLines("Table_Mult_Inst_Aux4.tex")[-(1:3)]
Table_Aux4 <- gsub("TIM - ITT", "TIM - ITT1", Table_Aux4)
Table_Aux5 <- readLines("Table_Mult_Inst_Aux5.tex")[-(1:3)]
Table_Aux5 <- gsub("TIM - ITT", "TIM - ITT2", Table_Aux5)
Table_Aux6 <- readLines("Table_Mult_Inst_Aux6.tex")[-(1:3)]
Table_Aux6 <- gsub("TIM - ITT", "TIM - ITT3", Table_Aux6)


Table_ml1 <- "& Total & Unit & Weight per & Shipments & Number of \\\\"
Table_ml2 <- "& Exports & Value & Shipment & per Buyer & Buyers & \\\\"
Table_ml4 <- "\\midrule"
Table_All <- c(
  Table_Aux1[1:2],
  Table_ml1, Table_ml2, Table_ml4,
  Table_Aux1[3:6],
  Table_Aux2[5:6],
  Table_Aux3[5:6],
  Table_Aux4[5:6],
  Table_Aux5[5:6],
  Table_Aux6[5:6], Table_ml4,
  Table_Aux1[7:12], Table_ml4,
  Table_Aux1[14], Table_ml4, Table_ml4,
  Table_Aux1[18]
)
Table_All
name <- paste(folder, "Table_13_Final.tex", sep = "")
writeLines(Table_All, name)

